package Dispatch.HttpHandleImpl;

import Dispatch.HttpHandle;
import HttpBean.Request.Request;
import HttpBean.Response.Response;
import HttpBean.Response.ResponseComponent;
import Utils.LoadHtmlFile;

public class HttpHandleStaticResourseImpl extends HttpHandle {
    @Override
    protected Response doResolve(Request request) {
        try {

            String str = LoadHtmlFile.load(request.getRequestLine().getUrl());
            Response response = ResponseComponent.get("200");
            response.initResponse(request);
            if(request.getRequestLine().getUrl().endsWith(".css")){
                response.setHeader("Content-Type", "text/css; charset=utf-8");
            }else if(request.getRequestLine().getUrl().endsWith(".js")){
                response.setHeader("Content-Type","application/javascript");
            }
            response.setEntity(str);
            return response;
        } catch (Exception e) {
            Response response = ResponseComponent.get("404");
            response.initResponse(request);
            try {
                response.setEntity(LoadHtmlFile.load("/404Error.html"));
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return response;
        }
    }
}
